<template>
  <div>
    <div class="advertisement-info">
      <div class="content">
        <div class="project-info">
          <div class="advertisement-form mt50">
            <el-form
              style="max-width: 1200px"
              :model="state.form"
              ref="formRef"
              :rules="rules"
              label-position="top"
            >
              <el-form-item label="协议类型" prop="leixing">
                <el-select
                  width="240"
                  show-border
                  v-model="state.form.leixing"
                  @change="select"
                  size="large"
                  placeholder="协议类型"
                >
                  <el-option
                    v-for="item in state.type"
                    :key="item.dicValue"
                    :label="item.dicLabel"
                    :value="item.dicValue"
                  >
                    {{ item.dicLabel }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="协议内容" prop="xieyineirong">
                <editor
                  :initValue="state.form.xieyineirong"
                  :index="index"
                  @onVal="onVal"
                />
              </el-form-item>
              <el-form-item class="button-group">
                <el-button
                  style="margin-left: 100px"
                  type="primary"
                  @click="submitForm(formRef)"
                  >提交</el-button
                >
                <el-button
                  type="primary"
                  style="margin-left: 50px"
                  @click="$router.go(-1)"
                  >取消</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetPrivacyDetail, EditPrivacy } from "@/api/system";
import { baseMsg } from "@/utils/utils";
import { GetDictionaryTree } from "@/api/system";
const route = useRoute();
const router = useRouter();
const formRef = ref(null);
const rules = {
  leixing: [{ required: true, message: "请选择协议类型", trigger: "change" }],
  xieyineirong: [
    { required: true, message: "请输入协议内容", trigger: "blur" },
  ],
};
const state = reactive({
  form: {
    id: route.query.id,
    xieyineirong: "",
    leixing: "",
  },
  type: [
    {
      dicLabel: "用户协议",
      dicValue: "yonghuyinsixieyi_yonghuxieyi",
    },
    {
      dicLabel: "隐私协议",
      dicValue: "yonghuyinsixieyi_yinsixieyi",
    },
    {
      dicLabel: "会员协议",
      dicValue: "yonghuyinsixieyi_huiyuanxieyi",
    },
  ],
});
// 查询协议信息
const GetInfo = () => {
  GetPrivacyDetail({ id: route.query.id }).then((res) => {
    if (res.code == 4201) {
      state.form = res.data;
    }
  });
  // GetDictionaryTree({ dicModel: "vrzhantingleixing" }).then((res) => {
  //   if (res.code == 4201) {
  //     state.type = res.data;
  //   }
  // });
};
// 富文本编辑器
const onVal = (val) => {
  state.form.xieyineirong = val;
};
// 提交表单
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      EditPrivacy(state.form).then((res) => {
        if (res.code == 4202) {
          baseMsg("编辑协议信息成功");
          router.go(-1);
        }
      });
    }
  });
};
GetInfo();
</script>
<style lang="scss" scoped>
.content {
  border: none !important;
}
.project-info {
  padding: 30px 100px;
  font-size: 20px;
  color: black;
  display: flex;
  .advertisement-form {
    width: 1200px;
  }
}
.el-form-item {
  display: flex;
  flex-direction: column;
  &.vue-editor {
    align-items: baseline;
  }
  &.status {
    flex-direction: row;
  }
}
</style>
<style lang="scss">
.advertisement-form {
  textarea {
    &[name="editorValue"] {
      display: none;
    }
  }
}
.button-group {
  margin-top: 50px;
  .el-form-item__content {
    width: 100% !important;
    justify-content: center;
  }
}
</style>
